package com.question.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.AjaxResponse;
import com.common.util.JpaPageable;
import com.common.util.TablePage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.question.entity.Blank;
import com.question.entity.Options;
import com.question.entity.dto.BlankDTO;
import com.question.service.IBlankService;
import com.question.service.IOptionsService;
import com.user.entity.dto.TeacherDTO;
import com.user.service.ITeacherService;

@Controller
@RequestMapping("/blank")
public class BlankController {
	@Autowired
	private IBlankService blankService;
	@Autowired
	private IOptionsService opService;
	@Autowired
	private ITeacherService teacherService;
	
	@RequestMapping("/saveOne")
	public void saveOne() {
		Blank b = new Blank();
		b.setTitle("这是content");
		Map<Integer, Options> m = new HashMap<Integer, Options>();
		Options o = new Options();
		o.setContent("填空1");
		m.put(1,o);
		b.setBlanks(m);
		blankService.saveOne(b);
	}
	@RequestMapping("/findAllTest")
	public @ResponseBody List<Blank> findAllTest(){
		return blankService.findAll();
	}
	@RequestMapping("/findPage")
	public @ResponseBody TablePage<Blank> findPage(JpaPageable pageable,HttpSession session,String str,Long subject_id){
		try {
			List<Long> ids = new ArrayList<Long>();
			
			TeacherDTO dto = (TeacherDTO) session.getAttribute("teacher");
			if(null!=dto)
				ids = teacherService.getSubjectsId(dto.getId());
			return blankService.findPage(BlankDTO.getSpecification(ids,str,subject_id), pageable.getPageable());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody AjaxResponse saveOrUpdate(String blank) {
		try {
			if(null!=blank) {
				ObjectMapper objectMapper = new ObjectMapper();
				Blank obj = objectMapper.readValue(blank,Blank.class);
				if(blankService.isNumExist(obj))
					return new AjaxResponse(false, "编号已存在!");
				if(obj.getId()!=null&&blankService.isUsing(obj.getId()))
					return new AjaxResponse(false, "题目已被使用，不允许修改!");
				blankService.saveOrUpdate(obj);
				return new AjaxResponse(true, "操作成功!");
			}else {
				return new AjaxResponse(false,"操作失败...");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败...");
		}
	}
	@RequestMapping("/delete")
	public @ResponseBody AjaxResponse delete(Long id) {
		try {
			if(blankService.isUsed(id))return new AjaxResponse(false, "题目已被使用！不能删除！");
			blankService.delete(id);
			return new AjaxResponse(true, "删除成功!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败...");
		}
	}
	@RequestMapping("/findOne")
	public @ResponseBody Blank findOne(Long id) {
		try {
			return blankService.findOne(id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
}
